﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using JKF.DataModule.SqlServerDAL;
using JKF.Entities.Model;
using JKF.Substructure;
using System.Data.Common;

namespace JKF.BusinessLayer.BLL
{
    public partial class bllCALL_LOG
    {
         /// <summary>
        ///  获取通话记录列表
        /// </summary>
        /// <param name="userinfo">当前用户</param>
        /// <param name="CARD_NO">卡号</param>
        /// <param name="NAME">姓名</param>
        /// <param name="PHONE">手机</param>
        /// <param name="LEVEL_NAME">级别名称</param>
        /// <param name="DATE_START">通话日期开始</param>
        /// <param name="DATE_END">通话日期结束</param>
        /// <param name="pageCurrent">当前页码</param>
        /// <param name="pageSize">一页显示多少</param>
        /// <param name="pageCount">总页数</param>
        /// <param name="rowCount">总记录数</param>
        /// <returns>返回DataTable结果集</returns>
        public DataTable GetCALL_LOGSBLL(SYS_USERinfo userinfo, string CARD_NO, string NAME, string PHONE, string LEVEL_NAME, string DATE_START, string DATE_END, string CUSTOMER_KEY, string ownShop, int pageCurrent, int pageSize, ref int pageCount, ref int rowCount)
        {
            return dalCALL_LOG.GetCALL_LOGSDAL(userinfo, CARD_NO, NAME, PHONE, LEVEL_NAME, DATE_START, DATE_END, CUSTOMER_KEY, ownShop, pageCurrent, pageSize, ref pageCount, ref rowCount);
        }
         /// <summary>
        /// 事务执行SQL语句
        /// </summary>
        /// <returns></returns>
        public  bool TransationSql(string sql)
        {
            return dalCALL_LOG.TransationSql(sql);
        }
        /// <summary>
        /// 通话记录添加
        /// </summary>
        /// <param name="model">通话记录实体</param>
        /// <returns></returns>
        public bool AddCallLogBLL(CALL_LOGinfo model)
        {
            string logContent = "";
            logContent = string.Format("添加通话记录，顾客姓名\"{0}\"  手机号\"{1}\"；",model.NAME,model.PHONE);
            dal.Add(model);
            return bllSYS_LOG.SaveLog(SysConfig.CALL_LOG_MODULE_CODE, SysConfig.CALL_LOG,model.ID.ToString(), logContent, SysConfig.Add, BusinessBase.ClientIp);
        }
        /// <summary>
        /// 通话记录修改
        /// </summary>
        /// <param name="model">通话记录实体</param>
        /// <returns></returns>
        public bool UpdateCallLogBLL(CALL_LOGinfo model)
        {
            string logContent = "";
            //1. 先获取到之前的记录
            CALL_LOGinfo oldModel = dal.GetModel(model.ID);
            logContent = string.Format("修改通话记录，顾客姓名\"{0}\"  手机号\"{1}\"：",oldModel.NAME,oldModel.PHONE);
            if (oldModel.NAME != model.NAME)
            {
                logContent += string.Format("顾客姓名由\"{0}\"改为\"{1}\"；", oldModel.NAME, model.NAME);
            }

            if (oldModel.SEX !=model.SEX)
            {
                logContent += string.Format("性别由\"{0}\"改为\"{1}\"；", oldModel.SEX, model.SEX);
            }
            if (oldModel.CALL_DATE != model.CALL_DATE)
            {
                logContent += string.Format("通话日期由\"{0}\"改为\"{1}\"；", oldModel.CALL_DATE, model.CALL_DATE);
            }
            if (oldModel.PHONE != model.PHONE)
            {
                logContent += string.Format("本次通话号码由\"{0}\"改为\"{1}\"；", oldModel.PHONE, model.PHONE);
            }
            if (oldModel.CONTENT != model.CONTENT)
            {
                logContent += string.Format("通话内容由\"{0}\"改为\"{1}\"；", oldModel.CONTENT, model.CONTENT);
            }
            if (oldModel.MEMO != model.MEMO)
            {
                logContent += string.Format("备注由\"{0}\"改为\"{1}\"；", oldModel.MEMO, model.MEMO);
            }
            //更新实体
            dal.Update(model);
            //写入操作日志表中
            return bllSYS_LOG.SaveLog(SysConfig.CALL_LOG_MODULE_CODE, SysConfig.CALL_LOG, model.ID.ToString(), logContent, SysConfig.Edit, BusinessBase.ClientIp);
        }

        /// <summary>
        /// 删除通话记录
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns></returns>
        public bool DeleteCallLogBLL(string id)
        {
            
            var dbClass = DB.HR;
            var cmdList = new List<DbCommand>();
            string logContent = "";

            try
            {
                var strSql = "update dbo.CALL_LOG set STATUS='9',DATE_UPDATE=getdate()  where ID=@ID"; 
                var cmd = dbClass.GetSqlStringCommand(strSql);
                dbClass.AddInParameter(cmd, "ID", DbType.Guid, new Guid(id));
                CALL_LOGinfo model = dal.GetModel(new Guid(id));
                logContent = string.Format("删除通话记录，顾客姓名\"{0}\"  手机号\"{1}\"：",model.NAME,model.PHONE); 
                cmdList.Add(cmd); 

                bllSYS_LOG.SaveLog(SysConfig.CALL_LOG_MODULE_CODE, SysConfig.CALL_LOG, id, logContent, SysConfig.Delete, BusinessBase.ClientIp);
                //执行逻辑删除
                ExecTransaction.Exec(cmdList);
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}
